﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace GPXCleaner
{
  public partial class Main : Form
  {
    private FileInfo _fi;
    private GpxData _data;
    private Airports _airports;

    public Main()
    {
      InitializeComponent();
    }

    private void AppendExceptionMessage(Exception exc, ref string text)
    {
      if (exc != null)
      {
        text += "\r\n" + exc.Message;
        AppendExceptionMessage(exc.InnerException, ref text);
      }
    }
    [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Windows.Forms.FileDialog.set_Title(System.String)")]
    [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
    [SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
    [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "gpx")]
    [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Windows.Forms.FileDialog.set_Filter(System.String)")]
    private void OpenGpxFile(GpxData.GPXVersion version)
    {
      using (OpenFileDialog dlg = new OpenFileDialog())
      {
        dlg.AddExtension = true;
        dlg.CheckFileExists = true;
        dlg.DefaultExt = ".gpx";
        dlg.Filter = "GPX Files (*.gpx)|*.gpx|All files (*.*)|*.*";
        dlg.FilterIndex = 0;
        dlg.RestoreDirectory = true;
        dlg.Title = "Open GPX File ...";
        dlg.ValidateNames = true;
        if (dlg.ShowDialog() == DialogResult.OK)
        {
          try
          {
            _fi = new FileInfo(dlg.FileName);
            _data = GpxData.FromFile(_fi, version);
          }
          catch (Exception exc)
          {
            string errorMsg = "Error: Could not read file from disk. Original error: ";
            AppendExceptionMessage(exc, ref errorMsg);
            MessageBox.Show(errorMsg);
          }
        }
      }
    }
    [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Windows.Forms.FileDialog.set_Title(System.String)")]
    [SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
    [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
    [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "gpx")]
    [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Windows.Forms.FileDialog.set_Filter(System.String)")]
    private void SaveGpxFile()
    {
      using (SaveFileDialog dlg = new SaveFileDialog())
      {
        dlg.AddExtension = true;
        dlg.DefaultExt = ".gpx";
        dlg.Filter = "GPX Files (*.gpx)|*.gpx|All files (*.*)|*.*";
        dlg.FilterIndex = 0;
        dlg.RestoreDirectory = true;
        dlg.Title = "Save GPX File ...";
        dlg.ValidateNames = true;
        if (dlg.ShowDialog() == DialogResult.OK)
        {
          try
          {
            FileInfo fi = new FileInfo(dlg.FileName);
            _data.ToFile(fi);
            Close();
          }
          catch (Exception exc)
          {
            string errorMsg = "Error: Could not write file to disk. Original error: ";
            AppendExceptionMessage(exc, ref errorMsg);
            MessageBox.Show(errorMsg);
          }
        }
      }
    }

    private void _openGpx10_Click(object sender, EventArgs e)
    {
      OpenGpxFile(GpxData.GPXVersion.v10);
    }
    private void _openGpx11_Click(object sender, EventArgs e)
    {
      OpenGpxFile(GpxData.GPXVersion.v10);
    }
    [SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
    [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
    private void _saveGpx_Click(object sender, EventArgs e)
    {
      try
      {
        _data.ProcessWayPoints(int.Parse(_maxNumWpt.Text, CultureInfo.InvariantCulture), _generatePrefix.Checked, _generateRating.Checked);
        SaveGpxFile();
      }
      catch (Exception exc)
      {
        string errorMsg = "Error: Could not process data. Original error: ";
        AppendExceptionMessage(exc, ref errorMsg);
        MessageBox.Show(errorMsg);
      }
    }

    [SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
    [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
    private void _airports_Ready(object sender, EventArgs e)
    {
      try
      {
        _data = GpxData.FromList(_airports.WayPoints);
        SaveGpxFile();
      }
      catch (Exception exc)
      {
        string errorMsg = "Error: Could not process data. Original error: ";
        AppendExceptionMessage(exc, ref errorMsg);
        MessageBox.Show(errorMsg);
      }
    }

    private void _readAirports_Click(object sender, EventArgs e)
    {
      _airports = new Airports(int.Parse(_maxNumWpt.Text, CultureInfo.InvariantCulture), _useSmallAirports.Checked);
      _airports.Ready += _airports_Ready;
    }
  }
}
